package com.sensePlusVR.manager;

import java.util.List;

import org.apache.http.Header;
import org.apache.http.entity.ByteArrayEntity;
import org.json.JSONObject;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.sensePlusVR.application.VRApplication;
import com.sensePlusVR.bean.BuriePointActiveReqDto;
import com.sensePlusVR.bean.BuriePointEventReqDto;
import com.sensePlusVR.net.NetCallBack;
import com.sensePlusVR.net.RequestUtils;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.ConstantURL;
import com.sensePlusVR.utils.MyLog;

/**
 * 埋点管理接口
 * @author Administrator
 * 
 * 先处理当每次启动应用则上传所有买点信息给服务器
 * 
 *
 *
 */
@SuppressWarnings({"deprecation" })
public class BuriedPointManager extends BaseManager{
	
	//FSDAFDS
	private static final String TAG = BuriedPointManager.class.getSimpleName();
	private static BuriedPointManager INSTANTS;
	
	
	static public BuriedPointManager getInstants(){
		if (INSTANTS == null){
			INSTANTS = new BuriedPointManager();
		}
		return INSTANTS;
	}
	
	/**
	 * 上传所有的买点信息给服务器
	 */
	public void uploadAllBuriedPointToService(){
		MyLog.d(TAG, "uploadAllBuriedPointToService");
		List<BuriePointActiveReqDto> buriePointActiveReqDtoList = BuriePointActiveReqDto.getAllBuriePointActiveReqDto();
		if(buriePointActiveReqDtoList != null && buriePointActiveReqDtoList.size() > 0){
			uploadActive(buriePointActiveReqDtoList);
		}
		
		List<BuriePointEventReqDto> buriePointEventReqDtoList = BuriePointEventReqDto.getAllSuccessBuriePointEventReqDto();
		if(buriePointEventReqDtoList != null && buriePointEventReqDtoList.size() > 0){
			uploadEvent(buriePointEventReqDtoList);
		}
		
	}
	
	
	/**
	 * 应用活跃：进入APP频率
	 */
	public void uploadActive(List<BuriePointActiveReqDto> buriePointActiveReqDtoList){
		UploadTask task = new UploadTask();
		task.startUploadActive(buriePointActiveReqDtoList);
	}
	
	
	/**
	 * 事件： GAME_DOWNLOAD VIDEO_DOWNLOAD GAME_PLAY VIDEO_PLAY RECHARGE CONSUME
	 * PRAISE FOLLOW COLLECT
	 */
	public void uploadEvent(List<BuriePointEventReqDto> buriePointEventReqDtoList){
		UploadTask task = new UploadTask();
		task.startUploadEvent(buriePointEventReqDtoList);
	}
	
	public class UploadTask{
		
		public UploadTask(){};
		
		
		
		public void startUploadActive(List<BuriePointActiveReqDto> buriePointActiveReqDtoList){
			
			String url = ConstantURL.BURIED_ACTIVE_UPLOAD;
			//String url = "http://192.168.10.97/logs-downtank/v1/active";
			//SerializerFeature.WriteMapNullValue,如果有null字段则不过滤该字段
			String json = JSON.toJSONString(buriePointActiveReqDtoList,SerializerFeature.WriteMapNullValue);
			
			MyLog.d(TAG, "startUploadActive json : " + json);
			
			RequestUtils.setBurieHeaders(VRApplication.getAppContext());
			
			MyLog.d(TAG, "startUploadActive url : " + url);
			
			RequestUtils.clientBuriedPost(VRApplication.getAppContext(), url, new ByteArrayEntity(json.getBytes()), Constant.STR_APPLICATION_JSON, new NetCallBack(){
				@Override
				public void onMySuccess(int statusCode, Header[] aheader,
						JSONObject jsonobject) {
					super.onMySuccess(statusCode, aheader, jsonobject);
					MyLog.d(TAG, "startUploadActive onMySuccess statusCode : " + statusCode);
					
					if(BuriePointActiveReqDto.getAllBuriePointActiveReqDto() != null){
						
						Log.d(TAG, "startUploadActive size dbstart : " + BuriePointActiveReqDto.getAllBuriePointActiveReqDto().size());
						
						BuriePointActiveReqDto.delAllData();
						
						Log.d(TAG, "startUploadActive size dbend : " + BuriePointActiveReqDto.getAllBuriePointActiveReqDto().size());
						
					}
					
				}
				
				@Override
				public void onMyFailure(int statusCode, Header[] headers,
						Throwable throwable, JSONObject errorResponse) {
					super.onMyFailure(statusCode, headers, throwable, errorResponse);
					MyLog.e(TAG, "startUploadActive onMyFailure statusCode : " + statusCode);
					
					
				}
				
			});
			
		}
		
		
		public void startUploadEvent(List<BuriePointEventReqDto> buriePointEventReqDtoList){
			
			String url = ConstantURL.BURIED_EVENT_UPLOAD;
			
			String json = JSON.toJSONString(buriePointEventReqDtoList,SerializerFeature.WriteMapNullValue);
			
			MyLog.d(TAG, "startUploadEvent json : " + json);
			
			RequestUtils.setBurieHeaders(VRApplication.getAppContext());
			
			MyLog.d(TAG, "startUploadEvent url : " + url);
			
			RequestUtils.clientBuriedPost(VRApplication.getAppContext(), url, new ByteArrayEntity(json.getBytes()), Constant.STR_APPLICATION_JSON, new NetCallBack(){
				@Override
				public void onMySuccess(int statusCode, Header[] aheader,
						JSONObject jsonobject) {
					super.onMySuccess(statusCode, aheader, jsonobject);
					MyLog.d(TAG, "startUploadEvent onMySuccess statusCode : " + statusCode);
					
					if(BuriePointEventReqDto.getAllSuccessBuriePointEventReqDto() != null){
						
						Log.d(TAG, "startUploadEvent size dbstart : " + BuriePointEventReqDto.getAllSuccessBuriePointEventReqDto().size());
						
						BuriePointEventReqDto.delAllSuccessData();
						
						Log.d(TAG, "startUploadEvent size dbend : " + BuriePointEventReqDto.getAllSuccessBuriePointEventReqDto().size());
					}
					
				}
				
				@Override
				public void onMyFailure(int statusCode, Header[] headers,
						Throwable throwable, JSONObject errorResponse) {
					super.onMyFailure(statusCode, headers, throwable, errorResponse);
					MyLog.e(TAG, "startUploadEvent onMyFailure statusCode : " + statusCode);
					
					
				}
				
			});
			
		}
		
	}
	
	

}
